﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace GalaxyPresident
{
    public class LivableBlock : Block
    {
        private float happiness = 100;
        private float environment = 200;
        private float population = 50;
        private float economic = 20;
        public void InitRandom()
        {
            happiness = 200 + Constant.random.Next(300);
            environment = 100 + Constant.random.Next(800);
            population = Constant.random.Next(200);
            economic = Constant.random.Next(200);
        }
        public float Happiness
        {
            get { return happiness; }
            set
            {
                if (value > 1000)
                {
                    happiness = 1000;
                    return;
                }
                if (value < 0)
                {
                    happiness = 0;
                    return;
                }
                happiness = value;
            }
        }

        public float Environment
        {
            get { return environment; }
            set
            {
                if (value > 1000)
                {
                    environment = 1000;
                    return;
                }
                if (value < 0)
                {
                    environment = 0;
                    return;
                }
                environment = value;
            }
        }

        public float Economic
        {
            get { return economic; }
            set
            {
                if (value > 1000)
                {
                    economic = 1000;
                    return;
                }
                if (value < 0)
                {
                    economic = 0;
                    return;
                }
                economic = value;
            }
        }

        public float Population
        {
            get { return population; }
            set
            {
                if (value > 1000)
                {
                    population = 1000;
                    return;
                }
                if (value < 0)
                {
                    population = 0;
                    return;
                }
                population = value;
            }
        }

        public LivableBlock(BBB owner, int blockIndex, PlanetEntity planetEntity)
            : base(owner, blockIndex, planetEntity)
        {
            InitRandom();
        }

        public LivableBlock(BBB owner, int blockIndex)
            : base(owner, blockIndex)
        {
            InitRandom();
        }

        public override void UpdateInfo(GameTime gameTime)
        {
            base.UpdateInfo(gameTime);
            _currentTotalTime += gameTime.ElapsedGameTime.Milliseconds;
            if (_currentTotalTime >= 100 * 1)
            {
                _currentTotalTime = 0;

                autoUpdateBlockInfo();

                if (IsJustDestroy)
                {
                    return;
                }
                autoBuildCity();
                autoBuildForest();
            }
        }

        private void autoUpdateBlockInfo()
        {
            //float envirIndex = calculateEnvironmentIndex();
            //float econoIndex = calculateEconomicIndex();
            //float populIndex = calculateEconomicIndex();
            //float happyIndex = calculateHappinessIndex();

            //if (envirIndex >= 0)
            //    environment++;
            //else
            //    environment--;

            //if (populIndex >= 0)
            //    population++;
            //else
            //    population--;

            //if (happyIndex >= 0)
            //    happiness++;
            //else
            //    happiness--;

            //if (econoIndex >= 0)
            //    economic++;
            //else
            //    economic--;
            Population += (happiness - 500) / 1000 * 0.5f * population +(environment-50)*0.05f;
            Environment -= (population - 50) * 0.08f;
            Happiness += (economic - 30) * 0.001f-(population-50)*0.5f;
            Economic += population * 0.04f - ((float)Constant.random.NextDouble() * economic * 0.1f);
        }

        //private float calculateEconomicIndex()
        //{
        //    return population + environment + happiness;
        //}

        //private float calculateHappinessIndex()
        //{
        //    return environment + economic - population;
        //}

        //private float calculatePopulationIndex()
        //{
        //    return -environment - happiness + economic;
        //}

        //private float calculateEnvironmentIndex()
        //{
        //    return -population - economic + happiness;
        //}

        private void autoBuildCity()
        {
            //if (Entity!= null && Entity.getObjectName() == @"Forest")
            //{
            //    if (population >= 10)
            //    {
            //        destroyEntity();
            //        City city = new City((Planet)_owner);
            //        _owner.addNewEntityAtBlock(city, _blockIndex);
            //    }
            //    return;
            //}
            if (Entity == null)
            {
                if (population >= 100 && economic >= 100)
                    _owner.addNewEntityAtBlock(new City((Planet)_owner), _blockIndex);
            }
            else
            {
                if (_entity.isCommonEntity())
                {
                    CommonEntity entity = (CommonEntity)_entity;
                    //if ((population >= 20 && population < 40 && entity.Level == 1) ||
                    //    (population >= 40 && entity.Level == 2))
                    //    entity.levelUp();
                    //if ((population >= 20 && population < 40 && entity.Level == 3) ||
                    //    (population < 20 && entity.Level == 2))
                    //    entity.levelDown();
                    if (population >= 300 && economic >= 500)
                        entity.Level = 1;
                    if (population >= 500 && economic >= 800)
                        entity.Level = 2;
                }
            }
        }

        private void autoBuildForest()
        {
            if (Entity == null)
            {
                if (environment >= 300)
                    _owner.addNewEntityAtBlock(new Forest((Planet)_owner), _blockIndex);
            }
            else
            {
                if (_entity.isCommonEntity())
                {
                    CommonEntity entity = (CommonEntity)_entity;
                    //if ((environment >= 20 && entity.Level == 0) || (environment >= 40 && entity.Level == 1))
                    //    entity.levelUp();
                    //if ((environment >= 20 && environment < 40 && entity.Level == 0) ||
                    //    (environment >= 40 && entity.Level == 1))
                    //    entity.levelUp();
                    //if ((environment >= 20 && environment < 40 && entity.Level == 2) ||
                    //    (environment < 20 && entity.Level == 1))
                    //    entity.levelDown();
                    if (environment >= 500)
                        entity.Level = 1;
                    if (environment >= 800)
                        entity.Level = 2;
                }
            }
        }

        public override float getEconomicIncreaseAmount()
        {
            if (Entity != null)
                return Entity.getEconomicIncreaseAmount();
            return 0;
        }
    }
}
